package edu.ustb.mis.dm.simpleTest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import edu.ustb.mis.dm.util.Utils;

public class Test {
	private static String[] getArray(final String value) {
		if (value.equals("0")) {
			return new String[]{"1", "0", "0", "0", "0"};
		} else if (value.equals("2")) {
			return new String[]{"0", "1", "0", "0", "0"};
		} else if (value.equals("4")) {
			return new String[]{"0", "0", "1", "0", "0"};
		} else if (value.equals("6")) {
			return new String[]{"0", "0", "0", "1", "0"};
		} else {
			return new String[]{"0", "0", "0", "0", "1"};
		}
	}

	public static void main(final String[] args) {
		try {
			final Reader reader = new BufferedReader(new FileReader("input/zoo.txt"));
			final List<List<String>> list = new ArrayList<List<String>>();
			String instanceString = Utils.readLine(reader);
			while (instanceString != null) {
				final List<String> tmp = new ArrayList<String>();
				for (final String s : instanceString.split(",")) {
					tmp.add(s);
				}
				list.add(tmp);
				instanceString = Utils.readLine(reader);
			}
			reader.close();
			final List<List<String>> list2 = new ArrayList<List<String>>();
			for (final List<String> li : list) {
				final String l12 = li.get(12);
				li.remove(12);
				li.set(15, li.get(16) + li.get(15));
				li.remove(16);
				final List<String> l2 = new ArrayList<String>();
				l2.add(li.get(li.size() - 1));
				l2.addAll(li.subList(0, 12));
				l2.addAll(Arrays.asList(getArray(l12)));
				l2.addAll(li.subList(12, 15));
				list2.add(l2);
			}
			for (final List<String> list3 : list2) {
				for (int i = 1; i < list3.size(); i++) {
					list3.set(i, String.valueOf( (Integer.valueOf(list3.get(i)) * i)));
				}
			}
			final BufferedWriter bw = new BufferedWriter(new FileWriter("output/zoo.txt"));
			final StringBuffer sbf = new StringBuffer();
			for (final List<String> ll : list2) {
				for (final String ss : ll) {
					if (!ss.equals("0")) {
						sbf.append(ss).append(",");
					}
				}
				sbf.append("\r\n");
			}
			bw.write(sbf.toString());
			bw.flush();
			bw.close();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}
}
